package com.example.demo.generator.scaffold.registry;

import com.example.demo.generator.scaffold.model.TemplateContext;
import com.example.demo.generator.scaffold.model.TemplateDescriptor;
import com.example.demo.generator.scaffold.util.ScaffoldNameUtils;

import java.util.List;

/**
 * 区块链相关模板集合。
 */
public final class BlockchainTemplates {

    private BlockchainTemplates() {
    }

    public static List<TemplateDescriptor> all() {
        return List.of(controller(), facade());
    }

    public static TemplateDescriptor controller() {
        return TemplateDescriptor.builder()
                .id("blockchain-controller")
                .templatePath("scaffold/templates/blockchain/controller.ftl")
                .outputPathResolver(context -> buildOutputPath(context, "integration.fisco", context.getClassInfo().getClassName() + "BlockchainController.java"))
                .build();
    }

    public static TemplateDescriptor facade() {
        return TemplateDescriptor.builder()
                .id("blockchain-facade")
                .templatePath("scaffold/templates/blockchain/facade.ftl")
                .outputPathResolver(context -> buildOutputPath(context, "integration.fisco", context.getClassInfo().getClassName() + "BlockchainFacade.java"))
                .build();
    }

    private static String buildOutputPath(TemplateContext context, String layer, String fileName) {
        String projectRoot = ScaffoldNameUtils.deriveArtifactId(context);
        String layerPath = layer.replace('.', '/');
        StringBuilder builder = new StringBuilder(projectRoot)
                .append("/src/main/java/")
                .append(context.getModulePath());
        if (!layerPath.isBlank()) {
            builder.append("/").append(layerPath);
        }
        builder.append("/").append(fileName);
        return builder.toString();
    }
}
